<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

/**
 * 信息模块表单请求验证
 * Class InfoRequest
 * @package App\Http\Requests
 */
class InfoRequest extends BaseRequest
{
    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'required|integer',
            'created_user_id' => 'required|integer',
            'info_category_id' => 'required|integer',
            'info_model_id' => 'required|integer',
            'title' => 'required|max:100',
            'subtitle' => 'nullable|max:50',
            'keywords' => 'nullable|max:500',
            'description' => 'nullable|max:200',
            'cover_image' => [
                function ($attribute, $value, $fail) {
                    if ($this->info_model_id == 2 && $value == 0) {
                        $fail(__('common.cover_image_tips'));
                    }
                    AttachmentRequest::validity(__('common.cover_image'), $value, $fail, 1);
                }
            ],
            'href' => 'nullable|max:100',
            'href_target' => 'nullable|max:100',
            'rel' => 'nullable|max:20',
            'hits' => 'nullable|integer|min:1',
            'recommend_type' => 'nullable|max:50',
            'top' => 'nullable|boolean',
            'status' => 'required|boolean',
            'created_at' => 'nullable',
            '*.id' => 'required|integer',
            '*.sort' => 'required|integer',
        ];
    }

    /**
     * 提示信息
     *
     * @return array
     */
    public function messages()
    {
        return [
        ];
    }

    /**
     * 定义场景
     *
     * @var \string[][]
     */
    public $scenes = [
        'store' => [
            'created_user_id',
            'info_category_id',
            'info_model_id',
            'title',
            'subtitle',
            'keywords',
            'description',
            'cover_image',
            'href',
            'href_target',
            'rel',
            'hits',
            'recommend_type',
            'top',
            'status',
            'created_at',
        ],
        'update' => [
            'title',
            'subtitle',
            'keywords',
            'description',
            'cover_image',
            'href',
            'href_target',
            'rel',
            'hits',
            'recommend_type',
            'top',
            'status',
            'created_at',
        ],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
        'updateSort' => ['*.id', '*.sort'],
        'updateTop' => ['top'],
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'info_category_id' => __('info.info_category_id'),
            'info_model_id' => __('info.info_model_id'),
            'title' => __('common.title'),
            'subtitle' => __('common.subtitle'),
            'keywords' => __('common.keywords'),
            'description' => __('common.description'),
            'cover_image' => __('common.cover_image'),
            'href' => __('common.href'),
            'href_target' => __('common.href_target'),
            'rel' => __('common.rel'),
            'hits' => __('common.hits'),
            'recommend_type' => __('common.recommend_type'),
            'top' => __('common.top'),
            'sort' => __('common.sort'),
            'status' => __('common.status')
        ];
    }


    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('created_user_id', Auth::id());
        $this->offsetSet('info_category_id', $this->info_category->id);
        $this->offsetSet('info_model_id', $this->info_category->info_model_id);
        if (isset($this->keywords)) $this->offsetSet('keywords', join(',', $this->keywords));
        if (isset($this->recommend_type)) $this->offsetSet('recommend_type', join(',', $this->recommend_type));
        if (!isset($this->created_at) || empty($this->created_at)) $this->offsetSet('created_at', Carbon::now());
        if (!isset($this->cover_image) || empty($this->cover_image)) $this->offsetSet('cover_image', 0);
    }
}
